

import { Logger } from "../../engine/Logger";
import { SpriteFrameComponent } from "../../engine/SpriteFrameComponent";
import UIManager from "../../engine/UIManager";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { GameConfig } from "../config/GameConfig";
import { RoomConfig } from "../config/RoomConfig";
import { RoomJinbiConfig } from "../config/RoomJinbiConfig";
import { ViewConfig } from "../config/ViewConfig";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewCoinRoom extends ViewBase {

    private gameType: number;
    private renshu: number;
    private rangpai: number;

    private changContainer: cc.Node

    public onInitChild(gameType: number, renshu: number, rangpai: number): void {
        this.gameType = gameType;
        this.renshu = renshu
        this.rangpai = rangpai
    }

    onEnable(): void {
        super.onEnable()

        let btnBack: cc.Node = this.findChild(this.node, "btnBack")
        UIHelper.onAddEvent(btnBack, () => {
            this.closeView()
            UIManager.instance.showView(ViewConfig.viewConfig.ViewCoinGame, null, this.gameType)
        })
        this.changContainer = this.findChild(this.node, "scrollView/view/content")
     
        SocketClient.req_jinbi_roomconfig(this.gameType, (dataList: Array<object>) => {
            RoomJinbiConfig.json2JinbiConfigList(dataList)
            this.refresh()
        })
        this.refresh()
    }

    private refresh() {
        Logger.log("configList==", RoomJinbiConfig.jinbiConfigList)
        for (let i = 0; i < 4; i++) {
            let config: RoomJinbiConfig = RoomJinbiConfig.jinbiConfigList[i]
            let chang: cc.Node = this.findChild(this.changContainer, "chang" + i)
            let txtMinScore: cc.Label = this.getChild(chang, "txtMinScore", cc.Label)
            let txtCost: cc.Label = this.getChild(chang, "txtCost", cc.Label)
            let txtOnline: cc.Label = this.getChild(chang, "txtOnline", cc.Label)
            let txtDifen:cc.Label = this.getChild(chang, "txtDifen", cc.Label)
            if (config) {
                txtMinScore.string = "" + config.enterMin
                txtCost.string = "" + config.serve
                txtOnline.string = "" + config.online
                txtDifen.string = ""+config.difen
                UIHelper.onAddEvent(chang, () => {
                    SocketClient.req_join_jinbi_room(this.gameType, RoomConfig.Room_Type.Jinbi, config.level, this.renshu, this.rangpai, () => {
                        this.closeView()
                    })
                }, cc.Button.Transition.NONE)
            }
        }
    }

}
